<?php
function get_type_name($type)
{
  $sql_types = "select * from tt_types where tt_types.id = ".(($type % 12) + 1);
  $tt_types = mysql_query($sql_types);
  print mysql_error(); 
  if(mysql_num_rows($tt_types)>0)
  {
    $tt_type = mysql_fetch_array($tt_types);
    return($tt_type['type']."<img src='/image/".$tt_type['img']."' width=32 height=32 alt='".$tt_type['type']."'>");
    #return($tt_type['type']);
  }
  else
  {
    return("");
  }
}

function print_card($card)
{
  $s = "";
  $sql_cards = "select tt_cards.*, 
                       tt_times.name time_name, 
                       tt_rares.name rare_name, 
                       tt_types.type type_name, 
                       tt_types.img type_img,
                       tt_classes.name class_name 
                from tt_cards,tt_times,tt_rares,tt_types,tt_classes 
                where tt_cards.id = ".$card." and 
                      tt_times.id = tt_cards.time and
                      tt_rares.id = tt_cards.rare and
                      tt_types.id = tt_cards.type and
                      tt_classes.id = tt_cards.class
                      ";
  $tt_cards = mysql_query($sql_cards);
  print mysql_error(); 
  if(mysql_num_rows($tt_cards)>0)
  {
    $tt_card = mysql_fetch_array($tt_cards);
    print mysql_error();
    $itm = explode("\\r\\n",$tt_card['des']);
    $s = "<table width='150' border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
    $s = $s."<TABLE border=0 width='100%'' cellpadding='2' cellspacing='1'>";
    $s = $s."<TR bgcolor='#003366'><TD notparsed>".$tt_card['id']."</TD><TD>".$tt_card['name']."</TD></TR>";
    for($i=0;$i<count($itm);$i++)
    {
      if($itm[$i]!="")
      {
        $s = $s."<TR bgcolor='#003366'><TD COLSPAN=2>".$itm[$i]."</TD></TR>";
      }
    }
    $s = $s."<TR bgcolor='#003366'><TD COLSPAN=2>
               [".$tt_card['type_name']."]<img src=/image/".$tt_card['type_img']." width=32 height=32 alt='".$tt_card['type_name']."'><br>
               [".$tt_card['rare_name']."]<br>
               [".$tt_card['time_name']."]<br>
               [".$tt_card['class_name']."]
               ";
    $sql_params = "select tt_cards_actions.*,tt_actions.action action_name
                   from tt_cards_actions,tt_actions
                   where tt_cards_actions.card = ".$tt_card['id']." and
                         tt_actions.id = tt_cards_actions.action
                         "; 
    $tt_params = mysql_query($sql_params);
    print mysql_error(); 
    if(mysql_num_rows($tt_params)>0)
    {
      while($tt_param = mysql_fetch_array($tt_params))
      {
        $s = $s."<br>".$tt_param['action_name'];
        if($tt_param['param']!="")
        {
          $s = $s."=".$tt_param['param'];
        }
      };
    }
    $s = $s."</TD></TR>";
    $s = $s."</TABLE>";
    $s = $s."</td></tr></table>";
  }
  return($s);
}

function print_herd_card($card)
{
  $s = "";
  $sql_cards = "select tt_games_cards.params card_params,
                       tt_cards.*, 
                       tt_times.name time_name, 
                       tt_rares.name rare_name, 
                       tt_types.type type_name, 
                       tt_types.img type_img,
                       tt_classes.name class_name,
                       tt_games_herds.nik nik 
                from tt_games_cards,tt_cards,tt_times,tt_rares,tt_types,tt_classes,tt_games_herds 
                where tt_games_cards.id = ".$card." and
                      tt_cards.id = tt_games_cards.card and 
                      tt_times.id = tt_cards.time and
                      tt_rares.id = tt_cards.rare and
                      tt_types.id = tt_cards.type and
                      tt_classes.id = tt_cards.class and
                      tt_games_herds.id = tt_games_cards.herd
                      ";
  $tt_cards = mysql_query($sql_cards);
  print mysql_error(); 
  if(mysql_num_rows($tt_cards)>0)
  {
    $tt_card = mysql_fetch_array($tt_cards);
    print mysql_error();
    $itm = explode("\\r\\n",$tt_card['des']);
    $s = " <table width='150' border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
    $s = $s."<TABLE border=0 width='100%'' cellpadding='2' cellspacing='1'>";
    $s = $s."<TR bgcolor='#003366'><TD notparsed>".$card."</TD><TD notparsed>".$tt_card['id']."</TD><TD>".$tt_card['name']."</TD></TR>";
    for($i=0;$i<count($itm);$i++)
    {
      if($itm[$i]!="")
      {
        $s = $s."<TR bgcolor='#003366'><TD COLSPAN=3>".$itm[$i]."</TD></TR>";
      }
    }
    $s = $s."<TR bgcolor='#003366'><TD COLSPAN=3>
               [".$tt_card['type_name']."]<img src=/image/".$tt_card['type_img']." width=32 height=32 alt='".$tt_card['type_name']."'><br>
               [".$tt_card['rare_name']."]<br>
               [".$tt_card['time_name']."]<br>
               [".$tt_card['class_name']."]
               ";
    $sql_params = "select tt_cards_actions.*,tt_actions.action action_name
                   from tt_cards_actions,tt_actions
                   where tt_cards_actions.card = ".$tt_card['id']." and
                         tt_actions.id = tt_cards_actions.action
                         "; 
    $tt_params = mysql_query($sql_params);
    print mysql_error(); 
    if(mysql_num_rows($tt_params)>0)
    {
      while($tt_param = mysql_fetch_array($tt_params))
      {
        $s = $s."<br>".$tt_param['action_name'];
        if($tt_param['param']!="")
        {
          $s = $s."=".$tt_param['param'];
        }
      };
    }
    $s = $s."</TD></TR>";
    $s = $s."</TABLE>";
    $s = $s."</td></tr></table>";
  }
  return($s);
}

function print_name_card($card)
{
  $s = "";
  $sql_cards = "select tt_cards.name 
                from tt_cards
                where tt_cards.id = ".$card;
  $tt_cards = mysql_query($sql_cards);
  print mysql_error(); 
  if(mysql_num_rows($tt_cards)>0)
  {
    $tt_card = mysql_fetch_array($tt_cards);
    print mysql_error();
    $s=$tt_card['name'];
  }
  return($s);
}

function sosed($vopr, $herd, $num)
{
 if($vopr==$herd)
 {
   return(100);
 }

 $rez = $herd - 1;
 if ($rez==0)
 {
   $rez = $num;
 }
 if($vopr==$rez)
 {
   return(1);
 }

 $rez = $herd + 1;
 if ($rez==($num + 1))
 {
   $rez = 1;
 }
 if($vopr==$rez)
 {
   return(1);
 }


 $rez = $herd - 2;
 if ($rez==0)
 {
   $rez = $num;
 }
 if ($rez==-1)
 {
   $rez = $num - 1;
 }
 if($vopr==$rez)
 {
   return(2);
 }

 $rez = $herd + 2;
 if ($rez==($num + 1))
 {
   $rez = 1;
 }
 if ($rez==($num + 2))
 {
   $rez = 2;
 }
 if($vopr==$rez)
 {
   return(2);
 }

 return(0);
}

?>